package com.vgmobile.cucrever2.product.detail;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.vgmobile.cucrever2.R;
import com.vgmobile.cucrever2.global.BaseActivity;
import com.viewpagerindicator.CirclePageIndicator;

public class ProductDetailActivity extends BaseActivity {
	
	private TextView txtTitle, txtProvider, txtName;
	private LinearLayout layoutProperties;
	private ViewPager imageViewPager;
	private CirclePageIndicator imageIndicator;
	private ImageDetailAdapter imageDetailAdapter;
	private LayoutInflater layoutInflater;
	
	private ArrayList<View> listItemColor;
	private int indexSelectColor;
	
	private TextView txtNumber;
	private int number;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.product_detail_activity);
		
		layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		listItemColor = new ArrayList<View>();
		
		imageViewPager = (ViewPager) findViewById(R.id.product_detail_image_viewpager);
		imageViewPager.setOffscreenPageLimit(0);
		imageIndicator = (CirclePageIndicator) findViewById(R.id.product_detail_image_indicator);
		imageIndicator.setFillColor(getResources().getColor(R.color.circle_indicator_fill_color));
		imageIndicator.setPageColor(getResources().getColor(R.color.circle_indicator_page_color));
		imageIndicator.setStrokeWidth(0);
		txtTitle = (TextView) findViewById(R.id.product_detail_bar_texttitle);
		txtTitle.setSelected(true);
		txtProvider = (TextView) findViewById(R.id.product_detail_provider);
		txtName = (TextView) findViewById(R.id.product_detail_name);
		layoutProperties = (LinearLayout) findViewById(R.id.product_detail_properties_layout);
		
		for (int i = 0; i < 5; i++) {
			View view = layoutInflater.inflate(R.layout.product_detail_properties, null);
			TextView text1 = (TextView) view.findViewById(R.id.product_detail_properties_text1);
			TextView text2 = (TextView) view.findViewById(R.id.product_detail_properties_text2);
			text1.setText("Nơi sản xuất");
			text2.setText("Tàu khựa");
			layoutProperties.addView(view);
		}
		
		ArrayList<String> listImage = new ArrayList<String>();
		for (int i = 0; i < 8; i++) {
			listImage.add("http://imgcr.vatgia.vn/pictures/phagia/fxi1364454406.jpg");
		}
		imageDetailAdapter = new ImageDetailAdapter(this, listImage);
		imageViewPager.setAdapter(imageDetailAdapter);
		imageIndicator.setViewPager(imageViewPager);
		imageIndicator.setCurrentItem(0);
		
		txtTitle.setText("Loa vi tính ISOLO chính hãng bảo hành 1 năm");
		txtName.setText("Loa vi tính ISOLO chính hãng bảo hành 1 năm");
		txtProvider.setText("Lê Bảo Minh Camera");
	}

	public void clickStartColor(View view){
		listItemColor.clear();
		View viewParent = layoutInflater.inflate(R.layout.product_detail_dialog_color_layout, null);
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setPositiveButton("Tiếp tục", new android.content.DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				clickNumber();
			}
		});
		LinearLayout linearLayout = (LinearLayout) viewParent.findViewById(R.id.product_detail_dialog_color_layout);
		for (int i = 0; i < 4; i++) {
			View viewChild = layoutInflater.inflate(R.layout.product_detail_dialog_color_item, null);
			linearLayout.addView(viewChild);
			LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT, 1.0f);
			viewChild.setLayoutParams(param);
			View itemLayout = viewChild.findViewById(R.id.product_detail_dialog_item_color_layoutcheck);
			listItemColor.add(itemLayout);
			itemLayout.setOnClickListener(new OnClickListener() {
				public void onClick(View mView) {
					setSelectColor(mView);
				}
			});
		}
		builder.setView(viewParent);
		builder.show();
	}
	
	private void setSelectColor(View item){
		for (int i = 0; i < listItemColor.size(); i++) {
			if(item.equals(listItemColor.get(i))){
				indexSelectColor = i;
				item.setSelected(true);
			}else{
				listItemColor.get(i).setSelected(false);
			}
		}
	}
	
	private void clickNumber(){
		View viewParent = layoutInflater.inflate(R.layout.product_detail_dialog_number_layout, null);
		txtNumber = (TextView) viewParent.findViewById(R.id.product_detail_dialog_number_text);
		number = 1;
		txtNumber.setText(String.valueOf(number));
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setPositiveButton("Tiếp tục", new android.content.DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				
			}
		});
		builder.setView(viewParent);
		builder.show();
	}
	
	public void clickMinus(View view){
		if(number > 1){
			number--;
			txtNumber.setText(String.valueOf(number));
		}
	}
	
	public void clickPlus(View view){
		number++;
		txtNumber.setText(String.valueOf(number));
	}
	
}
